#!/bin/bash
. ~/.bash_profile   

seqNo=$1
iCMD=$2
dbname=$3
dbuser=$4
dbpass=$5
echo $seqNo
echo $iCMD
echo $dbname
echo $dbuser
echo $dbpass
echo ${@:6}
SQLCODE=0
SQLERR="NO ERROR MSG"
READ=0
LOADED=0
REJECT=0
COMMIT=0
wlog()
{
  wlog_dt=`date "+%Y/%m/%d-%H:%M:%S" `
  echo "\n${wlog_dt} $1"
}
############################################################
# �����������������ݿ�
# ���÷���: dbconnect db2connstring
# ����˵����1�����Ӵ�ID
# ����ֵ˵���� 0�����ӳ��ܣ� 1������ʧ��
#       ��д��:   FengL
############################################################
dbconnect()
{
   wlog "====================connect to $1=======================" 
   wlog "db2 connect to $1"

   dbconnect_local_state=`db2 get connection state|grep -ic "Connectable and Unconnected"`
   
   if [ $dbconnect_local_state -ne 1 ]; then
     db2 connect reset
   fi
    echo "db2 connect to $1 user ${2} using ${3}"
   db2 connect to $1 user ${2} using ${3}
   return 0

}

execSql()
{
        wlog "${1}"
        _execSqResultValues=`db2 -x "${1}" `
				_execSqlresult=$?
				wlog "${_execSqResultValues}"
        if [ ${_execSqlresult} -ne 0 ] && [ ${_execSqlresult} -ne 1 ] ;then
						echo "$_execSqResultValues" | sed -n  -e "/SQL[0-9][0-9][0-9][0-9]N/p"  | read SQLERR
						return 1
				else 
						return 0
				fi
}



if [ "$iCMD" -eq "0" ];then
		echo ${dbname} ${dbuser} ${dbpass}
    dbconnect ${dbname} ${dbuser} ${dbpass}
    if [ $? -ne 0 ];then 
      #echo "$rtnMsg" | sed -n  -e "/SQL[0-9][0-9][0-9][0-9]N/p"  | read SQLERR
      echo "SQLCODE:[-1] V_ROWS_READ:[$READ] V_ROWS_REJECTED:[$REJECT] V_LOADED:[$LOADED] SQLERR:[$SQLERR]"
      exit 1
    fi
    echo "1____________________________________________________________"             
    #execSql "drop table ddd44"
                  #execSql "create table ddd4(aa in1t)"
                  #  if [ $? -ne 0 ];then
                  #  echo "aa"
                  #   echo "SQLCODE:[-1] V_ROWS_READ:[$READ] V_ROWS_REJECTED:[$REJECT] V_LOADED:[$LOADED] SQLERR:[$SQLERR]"
                  #               return 2
                  #  fi
   echo "2____________________________________________________________"     
    echo "db2 ${@:6}"
    echo "3____________________________________________________________"  
    SDATA=`db2 ${@:6}`
		if [ $? -eq 4  ]; then
            SQLCODE=-1
            SQLCODE=`echo "$SDATA" | sed -n -e 's/^.*sqlcode: \([-,0-9][0-9]*\).*/\1/p'`
            SQLERR=`echo "$SDATA" | sed -n  -e "/SQL[0-9][0-9][0-9][0-9]/p"`
            READ=`echo "$SDATA" | sed -n -e 's/^.*read         = \([-,0-9][0-9]*\).*/\1/p'`
            LOADED=`echo "$SDATA" | sed -n -e 's/^.*loaded       = \([-,0-9][0-9]*\).*/\1/p'`
            REJECT=`echo "$SDATA" | sed -n -e 's/^.*rejected     = \([-,0-9][0-9]*\).*/\1/p'`
            COMMIT=`echo "$SDATA" | sed -n -e 's/^.*committed    = \([-,0-9][0-9]*\).*/\1/p'`
            SQLCODE=4
            echo "SQLCODE:[$SQLCODE]@V_ROWS_READ:[$READ]@V_ROWS_REJECTED:[$REJECT]@V_LOADED:[$LOADED]@SQLERR:[$SQLERR]@seqNo:[$seqNo]"
                return 3
        else
            SQLCODE=`echo "$SDATA" | sed -n -e 's/^.*sqlcode: \([-,0-9][0-9]*\).*/\1/p'`
            SQLERR=`echo "$SDATA" | sed -n  -e "/SQL[0-9][0-9][0-9][0-9]/p"`
            READ=`echo "$SDATA" | sed -n -e 's/^.*read         = \([-,0-9][0-9]*\).*/\1/p'`
            LOADED=`echo "$SDATA" | sed -n -e 's/^.*loaded       = \([-,0-9][0-9]*\).*/\1/p'`
            REJECT=`echo "$SDATA" | sed -n -e 's/^.*rejected     = \([-,0-9][0-9]*\).*/\1/p'`
            COMMIT=`echo "$SDATA" | sed -n -e 's/^.*committed    = \([-,0-9][0-9]*\).*/\1/p'`
            SQLCODE=0
        echo "SQLCODE:[$SQLCODE]@V_ROWS_READ:[$READ]@V_ROWS_REJECTED:[$REJECT]@V_LOADED:[$LOADED]@SQLERR:[$SQLERR]@seqNo:[$seqNo]"
        fi
    db2 disconnect current
    echo "SQLCODE:[$SQLCODE] V_ROWS_READ:[$READ] V_ROWS_REJECTED:[$REJECT] V_LOADED:[$LOADED] SQLERR:[$SQLERR]"
    exit 0
fi


if [ "$iCMD" -eq "1" ];then

        db2 connect to ${dbname} user ${dbuser} using ${dbpass}
        
        db2 -v "runstats on table $5"

        db2 disconnect current

        exit 0
fi